<?php
abstract class Controller {
	protected $registry;	
    protected $app;
	protected $id;
	protected $layout;
	protected $template;
	protected $children = array();
	protected $data = array();
	
	public function __construct($registry, $app) {
		$this->registry = $registry;
        $this->app = $app;
	}
	
	public function __get($key) {
		return $this->registry->get($key);
	}
	
	public function __set($key, $value) {
		$this->registry->set($key, $value);
	}
			
	protected function forward($route, $args = array()) {
		return $this->app->action($route, $args);
	}

	protected function redirect($url, $status = 302) {
		header('Status: ' . $status);
		header('Location: ' . str_replace(array('&amp;', "\n", "\r"), array('&', '', ''), $url));
		exit();				
	}
	
	protected function getChild($child, $args = array()) {
		$action = $this->app->action($child, $args);	
        return $this->app->controller($action, true);
	}
	
	protected function render() {
		foreach ($this->children as $child) {
			$this->data[basename($child)] = $this->getChild($child);
		}

        $view = $this->app->view($this->template);    

        $view->set_data($this->data);

        return $view->render();
	}
}
?>
